use "D:\BaiduNetdiskWorkspace\EDUCATION\Economics\Econometrics\Introductory Econometrics A Modern Approach_Wooldrige\stata伍德里奇\MROZ.DTA"
sum lwage educ

* OLS Estimation
reg lwage educ

*      Source |       SS           df       MS      Number of obs   =       428
*-------------+----------------------------------   F(1, 426)       =     56.93
*       Model |  26.3264193         1  26.3264193   Prob > F        =    0.0000
*    Residual |  197.001022       426  .462443713   R-squared       =    0.1179
*-------------+----------------------------------   Adj R-squared   =    0.1158
*       Total |  223.327441       427  .523015084   Root MSE        =    .68003

*------------------------------------------------------------------------------
*       lwage |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
*-------------+----------------------------------------------------------------
*        educ |   .1086487   .0143998     7.55   0.000     .0803451    .1369523
*       _cons |  -.1851968   .1852259    -1.00   0.318    -.5492673    .1788736
*------------------------------------------------------------------------------
gen lneduc=ln(educ)  //教育年限取对数
reg lwage lneduc
/*
      Source |       SS           df       MS      Number of obs   =       428
-------------+----------------------------------   F(1, 426)       =     51.20
       Model |  23.9593557         1  23.9593557   Prob > F        =    0.0000
    Residual |  199.368085       426    .4680002   R-squared       =    0.1073
-------------+----------------------------------   Adj R-squared   =    0.1052
       Total |  223.327441       427  .523015084   Root MSE        =    .68411

------------------------------------------------------------------------------
       lwage |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      lneduc |   1.256152   .1755609     7.16   0.000      .911079    1.601226
       _cons |  -1.976988   .4438785    -4.45   0.000    -2.849452   -1.104523
------------------------------------------------------------------------------
*/


* IV Estimation
ivreg2 lwage (educ = fatheduc)

*IV排他性约束
reg lwage educ fatheduc               //fatheduc 的系数需要不显著，在控制educ的条件下

*IV-first stage
reg educ fatheduc
predict educ_hat1, xb                  //求拟合值
gen educ_hat2=9.799013+fatheduc*0.2824277
*IV-Second stage
reg lwage educ_hat2
*IV-opinon
ivreg2 lwage (educ = fatheduc) ,first  //汇报第一阶段详细结果
ivreg2 lwage (educ = fatheduc) ,ffirst //汇报第一阶段建议结果


*If conditions
ivreg2 lwage (educ = fatheduc) if city==1,first  
ivreg2 lwage (educ = fatheduc) if city==0,first  

*Other Commands
ivreghdfe lwage (educ = fatheduc) ,first 


**多元回归
*明瑟方程
reg lwage educ exper expersq            //加入经验的平方
* lwage= -.0008112 *expersq+ .0415665*exper

twoway (qfit lwage exper) //绘图
gen educsq=educ*educ
gen fatheducsq=fatheduc*fatheduc
ivreg2 lwage exper expersq (educ educsq= fatheduc fatheducsq)   //不是一个好的工具变量
